package com.chen.apply.controller;

import com.chen.apply.service.ApplyService;
import com.common.pojo.req.ApplySubmitReqDTO;
import com.common.pojo.req.ApplyUpdateReqDTO;
import com.common.pojo.resp.ApplyEditRespDTO;
import com.common.pojo.resp.ApplyInquiryRespDTO;
import com.common.pojo.resp.ApprovalRecordRespDTO;
import com.common.pojo.resp.PageResult;
import com.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/apply")
@Slf4j
public class ApplyController {

    @Autowired
    private ApplyService applyService;



    @PostMapping("/submit")
    public Result<Boolean> submitApply(@RequestBody ApplySubmitReqDTO applySubmitReqDTO) {
        boolean result = applyService.submitApply(applySubmitReqDTO);
        return Result.success(result);
    }

    @GetMapping("/approval-records/{id}")
    public Result<List<ApprovalRecordRespDTO>> getApprovalRecords(@PathVariable Long id) {
        try {
            List<ApprovalRecordRespDTO> records =
                    applyService.getApprovalRecordsFromApprovalService(id);
            return Result.success(records);
        } catch (Exception e) {
            log.error("获取审核记录失败: {}", e.getMessage(), e);
            return Result.error("获取审核记录失败");
        }
    }


    @GetMapping("/inquiry")
    public Result<PageResult<ApplyInquiryRespDTO>> getApplyInquiryRespDTO(
            @RequestParam String startDate,
            @RequestParam String endDate,
            @RequestParam int pageSize,
            @RequestParam int pageNum) {

        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        PageResult<ApplyInquiryRespDTO> result = applyService.getApplyInquiryRespDTO(start, end, pageNum, pageSize);
        return Result.success(result);
    }
    @GetMapping("/status/{applyId}")
    public Result<Integer> getStatusByApplyId(@PathVariable Integer applyId) {
        Integer status = applyService.getStatusByApplyId(applyId);
        return Result.success(status);
    }

    @PutMapping("/update")
    public Result<Boolean> updateApply(@RequestBody ApplyUpdateReqDTO applyUpdateReqDTO) {
        log.info("applies: {}",applyUpdateReqDTO);
        boolean result = applyService.updateApply(applyUpdateReqDTO);
        return Result.success(result);
    }

    @GetMapping("/edit/{id}")
    public Result<ApplyEditRespDTO> getApplyEditData(@PathVariable Integer id) {
        ApplyEditRespDTO result = applyService.getApplyEditData(id);
        return Result.success(result);
    }
}
